Docker-Compose搭建nginx

test-nginx.yml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#mkdir -p /data/test-nginx/{html,conf,conf.d,logs}
#启动前将nginx.conf 放到 /data/test-nginx/conf 目录下
#启动前将test-nginx.conf 放到 /data/test-nginx/conf.d 目录下
#docker-compose -f test-nginx.yml up -d
#docker-compose.yml 配置文件如下
version: "3"
services:
test-nginx:
image: nginx
ports:
- "80:80"
volumes:
- /data/test-nginx/html:/usr/share/nginx/html
- /data/test-nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- /data/test-nginx/conf.d:/etc/nginx/conf.d
- /data/test-nginx/logs:/var/log/nginx
restart: always
container_name: test-nginx
networks:
test-net:
ipv4_address: 173.100.0.3
networks:
test-net:
external: true

nginx.conf 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# nginx.conf 例:
user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}

test-nginx.conf 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server {
listen 80;
server_name localhost;

# rewrite ^(.*)$ https://www.vhxsl.com permanent;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

#若需要转发
location /zhuanfa/ {
proxy_pass http://192.168.1.139:80/zhuanfa/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
}

.将一个 index.html 放到 /data/test-nginx/html
index.html随便写都可以

在外部浏览器可以访问 http://ip:80/

一辈子很短,努力的做好两件事就好;
第一件事是热爱生活,好好的去爱身边的人;
第二件事是努力学习,在工作中取得不一样的成绩,实现自己的价值,而不是仅仅为了赚钱;

继开 wechat
欢迎加我的微信,共同交流技术